package com.qk.controller;

import com.qk.Dto.ClueDto;
import com.qk.anno.Log;
import com.qk.common.Result;
import com.qk.entity.Clue;
import com.qk.server.ClueServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/clues")
public class ClueController {
    @Autowired
    private ClueServer clueServer;
    //添加
    @Log
    @PostMapping
    public Result add(@RequestBody Clue clue){
        log.info("添加数据{}", clue);
        clue.setStatus(1);
        clue.setUpdateTime(LocalDateTime.now());
        clue.setCreateTime(LocalDateTime.now());
        clueServer.save(clue);
        return Result.success();
    }
    //查找列表数据
    @GetMapping
    public Result findClueByList(ClueDto clueDto){
        return Result.success(clueServer.findClueByList(clueDto));
    }
    //分配线索
    @Log
    @PutMapping("/assign/{clueId}/{userId}")
    public Result assign(@PathVariable Integer clueId, @PathVariable Integer userId){
        log.info("分配线索{}给{}", clueId, userId);
        Clue clue = new Clue();
        clue.setStatus(2);
        clue.setId(clueId);
        clue.setUserId(userId);
        clue.setUpdateTime(LocalDateTime.now());
        clueServer.updateById(clue);
        return Result.success();
    }
    //根据id查询线索
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("查询id为{}的线索", id);
        Clue clue = clueServer.getClueById(id);
        return Result.success(clue);
    }
    //跟进线索
    @Log
    @PutMapping()
    public Result updateTrack(@RequestBody Clue clue){
        log.info("跟进线索{}", clue);
        clueServer.updateClueTrackById(clue);
        return Result.success();
    }
    //伪线索处理
    @Log
    @PutMapping("/false/{id}")
    public Result falseClue(@PathVariable Integer id, @RequestBody ClueDto clueDto){
        log.info("处理id为{}的线索", id);
        clueServer.updatefalseClueTrackById(id,clueDto.getReason(),clueDto.getRemark());
        return Result.success();
    }
    //转商机
    @Log
    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id){
        log.info("转商机{}", id);
        clueServer.toBusiness(id);
        return Result.success();
    }
    @GetMapping("/pool")
    public Result findCluePoolList(ClueDto clueDto){
        log.info("查找线索池列表数据{}", clueDto);
        return Result.success(clueServer.findCluePoolList(clueDto));
    }
}
